package com.ccp.dev.qbdms.tag;

import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.ws.model.CodeZsj;
import com.ccp.dev.qbdms.ws.service.CodeZsjService;
import com.ccp.dev.system.consts.SecretUtil;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.DictionaryService;
import com.ccp.dev.system.service.SysUserService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.List;

public class SecSelectTag extends SimpleTagSupport {

	/**
	 * 数据来源，信息密级，默认
	 */
	private static final short DATA_SRC_INFOSEC=1;
	/**
	 * 数据来源，人员密级
	 */
	private static final short DATA_SRC_USERSEC=2;


	/**
	 * 过滤条件，全部
	 */
	private static final short DATA_FILTER_ALL=0;
	/**
	 * 过滤条件，根据系统密级过滤，默认
	 */
	private static final short DATA_FILTER_SYSTEM=1;
	/**
	 * 过滤条件，根据自身密级过滤
	 */
	private static final short DATA_FILTER_USER=2;

	/**
	 * 数据字典中信息密级节点名称，对应SYS_GL_TYPE表
	 */
	private static final String NODEKEY_INFO = "sjmj";
	/**
	 * 数据字典中人员密级节点名称，对应SYS_GL_TYPE表
	 */
	private static final String NODEKEY_USER = "rymj";
	/**
	 * 主数据中信息密级节点名称，对应CODE_ZSJ表
	 */
	private static final String CODE_TYPE = "信息涉密等级";

	/**
	 * 数据来源，默认取信息密级
	 */
	private short data_src = DATA_SRC_INFOSEC;
	/**
	 * 过滤条件，默认为根据系统密级过滤
	 */
	private short data_filter = DATA_FILTER_SYSTEM;

	/**
	 * 当前实体的密级，比如当前用户的密级，当前消息的密级等
	 */
	private int nowSec;

	/**
	 * 下拉列表控件的名称
	 */
	private String id;

	/**
	 * 下拉列表控件的名称
	 */
	private String name;

	/**
	 * 下拉列表控件的名称
	 */
	private String css;

	/**
	 * 下拉列表的宽度
	 */
	private int width;

	/**
	 * 是否可选
	 */
	private Boolean activity = true;

	/**
	 * 是否必填
	 */
	private Boolean required = true;

	/**
	 * 是否有空白选项，默认有
	 */
	private Boolean hasBlank = true;

	/**
	 * 验证
	 */
	private Boolean search = false;

	public Boolean getRequired() {
		return required;
	}

	public void setRequired(Boolean required) {
		this.required = required;
	}

	public short getData_src() {
		return data_src;
	}
	public void setData_src(short data_src) {
		this.data_src = data_src;
	}

	public int getNowSec() {
		return nowSec;
	}
	public void setNowSec(int nowSec) {
		this.nowSec = nowSec;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public Boolean getActivity() {
		return activity;
	}

	public void setActivity(Boolean activity) {
		this.activity = activity;
	}

	public Boolean getSearch() {
		return search;
	}

	public void setSearch(Boolean search) {
		this.search = search;
	}

	public Boolean getHasBlank() {
		return hasBlank;
	}

	public void setHasBlank(Boolean hasBlank) {
		this.hasBlank = hasBlank;
	}

	@Override
	public void doTag() throws JspException {
		JspWriter out=getJspContext().getOut();
		try{
			DictionaryService dictionaryService = (DictionaryService) AppUtil.getBean(DictionaryService.class);
			SysUserService sysUserService = (SysUserService) AppUtil.getBean(SysUserService.class);
			CodeZsjService codeZsjService = AppUtil.getBean(CodeZsjService.class);
			List<CodeZsj> zsjList = codeZsjService.getByCodeName(CODE_TYPE,null);
			String nodeKey="";
			//数据来源为信息密级
			if(data_src==DATA_SRC_INFOSEC){
				nodeKey = NODEKEY_INFO;
				//数据来源为人员密级
			}else if(data_src==DATA_SRC_USERSEC){
				nodeKey = NODEKEY_USER;
			}
			List<Dictionary> list=dictionaryService.getByNodeKey(nodeKey);
			//取用户密级，根据用户密级过滤
			String userId = ContextUtil.getCurrentUserId();
			SysUser sysUser = sysUserService.getById(userId);
			StringBuffer content = new StringBuffer("<select ");
			if(id != null) {
				content.append("id=\"" + id + "\" ");
			}
			if(name != null) {
				content.append("name=\"" + name + "\" ");
			}
			if(css != null) {
				content.append("class=\"" + css + "\" ");
			}
			if(width != 0){
				content.append("style=\"width:" + width + "px\" ");
			}
			if(search){
				content.append(" lay-search ");
			}
			if(!activity){
				content.append(" disabled ");
			}
			if(required){
				content.append(" lay-verify=\"required\" ");
			}
			content.append("lay-reqtext=\"密级是必填项\"");
			content.append(">");

			if (hasBlank) {
				content.append("<option value=\"\"></option>");
			}
			if (data_src==DATA_SRC_INFOSEC){
				if(data_filter==DATA_FILTER_ALL){
					for(CodeZsj zsj :zsjList){
						content.append("<option value="+zsj.getCode());
						//默认本身密级
						if(nowSec==new Integer(zsj.getCode())){
							content.append(" selected ");
						}
						content.append(">"+zsj.getName()+"</option>");
					}
				}else{
					if(nowSec == 0 ){
						nowSec = ZhiLiangConst.DEFAULT_SECRET;
					}
					for (CodeZsj zsj : zsjList) {
						if (Integer.valueOf(zsj.getCode()) > SecretUtil.getUserDataSecretMap().get(sysUser.getPrivacyLevel())) {
							continue;
						}
						content.append("<option value=" + zsj.getCode());
						//默认本身密级
						if (nowSec == new Integer(zsj.getCode())) {
							content.append(" selected ");
						}
						content.append(">" + zsj.getName() + "</option>");
					}
				}
			}else if(data_src==DATA_SRC_USERSEC){
				for(Dictionary dic :list){
					if(new Integer(dic.getItemValue())>sysUser.getPrivacyLevel()){
						continue;
					}
					content.append("<option value="+dic.getItemValue());
					if(nowSec==new Integer(dic.getItemValue())){
						content.append(" selected ");
					}
					content.append(">"+dic.getItemName()+"</option>");
				}
			}

			content.append("</select>");
			out.print(content);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}
